package com.ruoyi.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.example.domain.Example;
import com.ruoyi.example.service.IExampleService;
import com.ruoyi.exapmle.service.IExamplePicService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 案例管理Controller
 * 
 * @author 王强
 * @date 2024-01-20
 */
@RestController
@RequestMapping("/frist/example")
public class ExampleController extends BaseController
{
    @Autowired
    private IExampleService exampleService;
    
    IExamplePicService picService;
    
    @Autowired
    public void setPicService(IExamplePicService picService) {
    	this.picService = picService;
    }

    /**
     * 查询案例管理列表
     */
    @PreAuthorize("@ss.hasPermi('frist:example:list')")
    @GetMapping("/list")
    public TableDataInfo list(Example example)
    {
        startPage();
        List<Example> list = exampleService.selectList(example);
        return getDataTable(list);
    }

    /**
     * 导出案例管理列表
     */
    @PreAuthorize("@ss.hasPermi('frist:example:export')")
    @Log(title = "案例管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Example example)
    {
        List<Example> list = exampleService.selectList(example);
        ExcelUtil<Example> util = new ExcelUtil<Example>(Example.class);
        util.exportExcel(response, list, "案例管理数据");
    }

    /**
     * 获取案例管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('frist:example:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(exampleService.selectById(id));
    }

    /**
     * 新增案例管理
     */
    @PreAuthorize("@ss.hasPermi('frist:example:add')")
    @Log(title = "案例管理", businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional
    public AjaxResult add(@RequestBody Example example)
    {
        int insert = exampleService.insert(example);
        
        picService.batchInsert(example.getMgExamplePicList(), insert);
        
		return toAjax(insert);
    }

    /**
     * 修改案例管理
     */
    @PreAuthorize("@ss.hasPermi('frist:example:edit')")
    @Log(title = "案例管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @Transactional
    public AjaxResult edit(@RequestBody Example example)
    {
    	
        int update = exampleService.update(example);
        
        picService.parseUpdate(example.getMgExamplePicList());
        
		return toAjax(update);
    }

    /**
     * 删除案例管理
     */
    @PreAuthorize("@ss.hasPermi('frist:example:remove')")
    @Log(title = "案例管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(exampleService.deleteByIds(ids));
    }
}
